package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * AddIncome Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by: Tan Si Ling (A0085314J)
 */

import shared.Income;
import cli.tools.Process;


public class AddIncome extends Function{

	private static String[] ADDINCOME = {
		"-= Main menu > Add Entry > Income =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu [e]: exit",
		"Enter the information in the following format: ",
		"Name / Price / Date (DD.MM.YYYY, optional) / Description (optional)",
		"",
	    "Input: " };


	private static final State BACK = State.ADDENTRYMENU;
	private static final State CURRENT = State.ADDINCOME;

	public static State runMenu() {

		String input = null;
		while(input == null || input.length() == 0){
		cli.print(ADDINCOME);
		input = cli.nextLine();
		cli.clearScreen();
		}

		return processInput(input);
	}

	// helper function to handle user input
	private static State processInput(String input) {
		
		if(input.length() == 1){
		char charInput = input.charAt(0);

		//check if it is a hotkey
		boolean isHotKey = tools.isHotKey(charInput);

		if(isHotKey){
			return hotKeyFunction(charInput);
		}
		}
		
		return normFunction(input);		


	}


	public static State normFunction(String input){

		//int array to store day, month and year
		int[] dmy = tools.initDate();

		// process user input
		String[] processing = input.split("/");
		// checks if input is valid : at least 2 "attributes"
		if (processing.length < 2){
			cli.print(INSUFFINFO);	
			return CURRENT;
		}
		
		String name = processing[0];
		String price = processing[1];
		String details = "";
		// storage for 3rd entry - need to see if 3rd entry is date/details
		String unknown;

		// checks price for validity and conversion
		int amount = tools.Money2int(price);

		if (amount <= 0){
			cli.print(BADAMT);
			return State.ADDINCOME;
		}
		
		if (processing.length >= 3) {
			//check if the 3rd entry is a date or detail
			unknown = processing[2];

			// checks if it is a date
			try {
				dmy = tools.processDate(unknown);
			} catch (Exception parseException) {
				// means it is not a valid date
				cli.print(INVALIDDATE);
				dmy = tools.initDate();
				// treat 3rd entry as details
				details = unknown + " ";
			}

		}
		
		// checks if there is a details section
		// since user may use '/' characters, we will treat anything
		// from
		// index 3 onwards as details
		if (processing.length >= 4) {
			for (int i = 3; i < processing.length; i++)
				details += processing[i] + " ";
		}
		

		try{
			// now, input has been processed and treated to be passed to api			
			Income income = cli.getApi().addIncome(name, amount, dmy[0], dmy[1], dmy[2],
					details);
			
			// Si Ling: **Keeping Track of the Actions DONE**
			tools.action.addToUndoStack(null, income,
					Process.ADDINCOME, null);
			// **END**
		}
		catch(Exception e){
			cli.print(ERROR);
		}
		return CURRENT;

	}


	private static State hotKeyFunction(char charInput){

		State nextState = CURRENT;

		switch (charInput) {
		case 'b':
		case 'B':
			nextState = BACK;
			break;
		case 'u':
		case 'U':
			tools.undo();
			break;
		case 'r':
		case 'R':
			tools.redo();
			break;
		case 'h':
		case 'H':
			nextState = State.MAINMENU;
			break;
		default:
			break;
		}

		return nextState;

	}



}